package com.icinfo.cloud.provider.punish.api.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.YesOrNoEnum;
import com.icinfo.cloud.provider.punish.api.dto.*;
import com.icinfo.cloud.provider.punish.api.service.IPunishCaseMainInfoService;
import com.icinfo.cloud.provider.punish.api.vo.*;
import com.icinfo.cloud.provider.punish.approval.dto.MemberUserVo;
import com.icinfo.cloud.provider.punish.approval.mapper.CaseApprovalWaitRecordMapper;
import com.icinfo.cloud.provider.punish.config.model.SysDepartParam;
import com.icinfo.cloud.provider.punish.config.service.ISysDepartParamService;
import com.icinfo.cloud.provider.punish.config.vo.LineCaseHandleInfoVo;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CasePartiesRecordQueryDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.PartyTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.*;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.*;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CasePartiesInfoVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.mybatis.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhengqiang
 * @description
 * @date 2023/4/18
 */
@Service
public class PunishCaseMainInfoServiceImpl implements IPunishCaseMainInfoService {

    @Resource
    private CaseMainInfoMapper caseMainInfoMapper;
    @Resource
    CaseHandlePersonRecordMapper caseHandlePersonRecordMapper;
    @Resource
    private CasePartiesRecordMapper casePartiesRecordMapper;
    @Resource
    private CaseApprovalWaitRecordMapper caseApprovalWaitRecordMapper;
    @Resource
    private CaseMainDetailInfoMapper caseMainDetailInfoMapper;
    @Resource
    private CaseDocumentRecordMapper caseDocumentRecordMapper;
    @Resource
    private ISysDepartParamService sysDepartParamService;


    /**
     * 【V1.11.4】描述：案件列表分页查询(成品油)
     *
     * @author: zhengqiang
     * @date: 2023/4/18
     * @param: queryDto
     * @return {@link List<  CaseInfoPageListForOilVo >}
     **/
    @Override
    public List<CaseInfoPageListForOilVo> getCaseInfoPageListForOilManage(CaseInfoPageListForOilQueryDto queryDto) {
        PageHelper.startPage(queryDto.getPageNum(), queryDto.getPageSize());
        List<CaseInfoPageListForOilVo> caseInfoPageListVos = caseMainInfoMapper.selectCaseInfoPageListForOilManage(queryDto);
        caseInfoPageListVos.forEach(e -> {
            //循环设置办案人员
            Example example = new Example(CaseHandlePersonRecord.class);
            example.createCriteria().andEqualTo("caseId", e.getCaseId()).andEqualTo("status",Constant.STRING_1);
            example.orderBy("handlePersonType").asc();
            List<CaseHandlePersonRecord> handlePersonRecords = caseHandlePersonRecordMapper.selectByExample(example);
            String handPersonName = handlePersonRecords.stream().map(p -> p.getHandlePersonName()).collect(Collectors.joining("、"));
            e.setHandlePerson(handPersonName);
        });
        return caseInfoPageListVos;
    }

    /**
     * 【V1.11.4】描述：根据案件编号列表查询案件列表(成品油)
     *
     * @author: zhengqiang
     * @date: 2023/4/18
     * @param: queryDto
     * @return {@link List<  CaseInfoPageListForOilVo >}
     **/
    @Override
    public List<CaseInfoPageListForOilVo> getCaseInfoListByCaseNoListForOilManage(CaseInfoListForOilQueryDto queryDto) {
        CaseInfoPageListForOilQueryDto pageListForOilQueryDto = new CaseInfoPageListForOilQueryDto();
        BeanUtils.copyProperties(queryDto, pageListForOilQueryDto);
        List<String> caseNoList = Arrays.asList(queryDto.getCaseNoList().split(","));
        pageListForOilQueryDto.setCaseNumberList(caseNoList);
        List<CaseInfoPageListForOilVo> caseInfoPageListVos = caseMainInfoMapper.selectCaseInfoPageListForOilManage(pageListForOilQueryDto);
        caseInfoPageListVos.forEach(e -> {
            //循环设置办案人员
            Example example = new Example(CaseHandlePersonRecord.class);
            example.createCriteria().andEqualTo("caseId", e.getCaseId()).andEqualTo("status",Constant.STRING_1);
            example.orderBy("handlePersonType").asc();
            List<CaseHandlePersonRecord> handlePersonRecords = caseHandlePersonRecordMapper.selectByExample(example);
            String handPersonName = handlePersonRecords.stream().map(p -> p.getHandlePersonName()).collect(Collectors.joining("、"));
            e.setHandlePerson(handPersonName);
        });
        return caseInfoPageListVos;
    }

    /**
     * 【V1.11.4】描述：根据登录人及案件名称查询案件列表(成品油)
     *
     * @author: zhengqiang
     * @date: 2023/4/18
     * @param: queryDto
     * @return {@link List< CaseNameAndNoListForOilVo>}
     **/
    @Override
    public List<CaseNameAndNoListForOilVo> getCaseNameAndNoListForOilManage(CaseNameAndNoListForOilQueryDto queryDto) {
        List<CaseNameAndNoListForOilVo> caseNameAndNoListForOilVos = caseMainInfoMapper.selectCaseNameAndNoListForOilManage(queryDto);
        return caseNameAndNoListForOilVos;
    }

    /**
     * 【V1.11.4】描述：获取案件当事人信息(成品油)
     *
     * @author: zhengqiang
     * @date: 2023/4/18
     * @param: queryDto
     * @return {@link Result < CasePartyInfoForOilVo>}
     **/
    @Override
    public CasePartyInfoForOilVo getCasePartyListForOilManage(CasePartyListForOilQueryDto queryDto) {
        CasePartyInfoForOilVo casePartyInfoForOilVo = new CasePartyInfoForOilVo();
        List<CasePartyPersonForOilVo> partyPersonForOilVoList = new ArrayList<>();
        List<CasePartyEntForOilVo> partyEntForOilVoList = new ArrayList<>();

        CasePartiesRecordQueryDto partiesRecordQueryDto = new CasePartiesRecordQueryDto();
        partiesRecordQueryDto.setIsQueryInvalidData(YesOrNoEnum.NO.getCode());
        partiesRecordQueryDto.setCaseId(queryDto.getCaseId());
        List<CasePartiesInfoVo> casePartiesInfoVos = casePartiesRecordMapper.selectCasePartiesInfoList(partiesRecordQueryDto);
        casePartiesInfoVos.forEach(e -> {
            if (PartyTypeEnum.PERSON.getCode().equals(e.getPartyType())) {
                CasePartyPersonForOilVo partyPerson = new CasePartyPersonForOilVo();
                partyPerson.setName(e.getPartyName());
                partyPerson.setIdNum(e.getPartyIdentityCode());
                partyPerson.setGender(e.getSex());
                partyPerson.setBirth(e.getBirth());
                partyPerson.setAddress(e.getAddress());
                partyPerson.setContactPhone(e.getContactPhone());
                partyPerson.setEntName(e.getWorkUnitName());
                partyPersonForOilVoList.add(partyPerson);
            } else {
                CasePartyEntForOilVo partyEnt = new CasePartyEntForOilVo();
                partyEnt.setEntName(e.getPartyName());
                partyEnt.setAttnName(e.getLegalName());
                partyEnt.setJobTitle(e.getJob());
                partyEnt.setAddress(e.getAddress());
                partyEnt.setUniscid(e.getPartyIdentityCode());
                partyEnt.setContactPhone(e.getContactPhone());
                partyEntForOilVoList.add(partyEnt);
            }
        });

        casePartyInfoForOilVo.setPerson(partyPersonForOilVoList);
        casePartyInfoForOilVo.setEnt(partyEntForOilVoList);
        CaseMainDetailInfo caseMainDetailInfo=caseMainDetailInfoMapper.selectByPrimaryKey(queryDto.getCaseId());
        casePartyInfoForOilVo.setCaseConciseCondition(caseMainDetailInfo.getCaseConciseCondition());
        return casePartyInfoForOilVo;
    }
    /**
     * 根据角色id与部门编码获取用户信息
     * @author liyafeng
     * @date 2023/4/19
      * @param queryDto
     * @return {@link List< MemberUserVo>}
     */
    @Override
    public List<MemberUserVo> getUserListForOilManageByRole(UserListForOilQueryDto queryDto) {

        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("orgCode",queryDto.getOrgCode());
        paramMap.put("roleId",queryDto.getRoleId());
        //根据机构、角色查询用户
        List<MemberUserVo> memberUserVos = caseApprovalWaitRecordMapper.selectMemberUserVoList(paramMap);
        return memberUserVos;
    }

    /**
     * 描述： 分页查询实施行政强制措施决定书记录
     *
     * @author: zhengqiang
     * @date: 2023/5/24
     * @param: pageListQueryDto
     * @return {@link List< CaseEnforceDocForOilVo>}
     **/
    @Override
    public List<CaseEnforceDocForOilVo> getCaseEnforceDocPageListForOilManage(CaseEnforceDocPageListForOilQueryDto pageListQueryDto) {
        PageHelper.startPage(pageListQueryDto.getPageNum(), pageListQueryDto.getPageSize());
        return caseDocumentRecordMapper.selectCaseEnforceDocList(pageListQueryDto);
    }

    /**
     * 描述： 根据部门编码查询条线配置信息
     *
     * @author: zhengqiang
     * @date: 2023/5/24
     * @param: queryDto
     * @return {@link List< LineInfoForOilVo>}
     **/
    @Override
    public List<LineInfoForOilVo> getLineInfoForOilManage(LineInfoListForOilQueryDto queryDto) {
        List<LineInfoForOilVo> lineInfoVoList = new ArrayList<>();
        SysDepartParam sysDepartParam = sysDepartParamService.getSysDepartParamByOrgCodeAndDeptCode(queryDto.getOrgCode(), "");
        if (ObjectUtil.isEmpty(sysDepartParam)) {
            return lineInfoVoList;
        }
        String lineConfig = sysDepartParam.getLineConfig();
        List<LineCaseHandleInfoVo> lineCaseHandleInfoVos = sysDepartParamService.assembleLineConfigToList(lineConfig);
        if (ObjectUtil.isEmpty(lineCaseHandleInfoVos)) {
            return lineInfoVoList;
        }
        lineCaseHandleInfoVos.forEach(lineCaseHandleInfoVo -> {
            //未开启 过滤
            if(Constant.STRING_0.equals(lineCaseHandleInfoVo.getLineCaseHandleStatusCode())) {
                return;
            }
            if (ObjectUtil.isEmpty(queryDto.getLineName())
                    || queryDto.getLineName().equals(lineCaseHandleInfoVo.getLineName())) {
                LineInfoForOilVo lineInfoVo = new LineInfoForOilVo();
                lineInfoVo.setLineCode(lineCaseHandleInfoVo.getLineCode());
                lineInfoVo.setLineName(lineCaseHandleInfoVo.getLineName());
                lineInfoVo.setDocumentNumberShortName(lineCaseHandleInfoVo.getLineDocNumber());
                lineInfoVo.setOrgStandardName(sysDepartParam.getOrgStandardName());
                lineInfoVoList.add(lineInfoVo);
            }
        });
        return lineInfoVoList;
    }

}
